#include "rclcpp/rclcpp.hpp"
#include "rclcpp/timer.hpp"
#include <chrono>

using namespace std::chrono_literals;
class MultiTimerNode : public rclcpp::Node {
public:
  MultiTimerNode(std::string name) : Node(name) {
    RCLCPP_INFO(this->get_logger(), "Start up node : %s", name.c_str());
    timer1_ = this->create_wall_timer(
        1s, std::bind(&MultiTimerNode::timer1_callback, this));
    timer2_ = this->create_wall_timer(
        10s, std::bind(&MultiTimerNode::timer2_callback, this));
    pauseTimer2();
  }
  void timer1_callback() {
    RCLCPP_INFO(this->get_logger(), "Timer 1 callback");
    if ((count_ % 5) == 0) {
      pauseTimer1();
      restartTimer2();
    }
    count_++;
  }
  void timer2_callback() {
    RCLCPP_INFO(this->get_logger(), "Timer 2 callback");
    if ((count_ % 10) == 0) {
      pauseTimer2();
      restartTimer1();
    }
    count_++;
  }
  //暂停定时器
  void pauseTimer1() {
    if (!timer1_->is_canceled()) {
      timer1_->cancel();
    }
  }
  void pauseTimer2() {
    if (!timer2_->is_canceled()) {
      timer2_->cancel();
    }
  }
  //这两个接口有啥用？
  void callTimer1() { timer1_->call(); }
  void callTimer2() { timer2_->call(); }
  //重启定时器
  void restartTimer1() { timer1_->reset(); }
  void restartTimer2() { timer2_->reset(); }

private:
  rclcpp::TimerBase::SharedPtr timer1_;
  rclcpp::TimerBase::SharedPtr timer2_;
  int count_ = 1;
};

int main(int argc, char *argv[]) {
  rclcpp::init(argc, argv);
  auto node = std::make_shared<MultiTimerNode>("start_pause_stop_timer_node");
  //   auto executor = rclcpp::executors::MultiThreadedExecutor();
  //   executor.add_node(node);
  //   executor.spin();
  rclcpp::spin(node);
  rclcpp::shutdown();
  return 0;
}